package com.pb.wkflow.core.camunda.bpmn.instanceImpl;

import com.pb.wkflow.core.camunda.bpmn.instance.Priority;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.model.bpmn.impl.instance.BpmnModelElementInstanceImpl;
import org.camunda.bpm.model.xml.ModelBuilder;
import org.camunda.bpm.model.xml.impl.instance.ModelTypeInstanceContext;
import org.camunda.bpm.model.xml.type.ModelElementTypeBuilder;
import org.camunda.bpm.model.xml.type.attribute.Attribute;

import static org.camunda.bpm.model.bpmn.impl.BpmnModelConstants.CAMUNDA_NS;

/**
 * @author 苗益辉
 * @date 2022/5/28 16:20
 */
@Slf4j
public class PriorityImpl extends BpmnModelElementInstanceImpl implements Priority {

    protected static Attribute<Integer> orderAttribute;

    public PriorityImpl(ModelTypeInstanceContext instanceContext) {
        super(instanceContext);
    }

    public static void registerType(ModelBuilder modelBuilder) {
        log.info("注册自定义连接线优先级 Priority");
        ModelElementTypeBuilder typeBuilder = modelBuilder.defineType(Priority.class, "priority")
                .namespaceUri(CAMUNDA_NS)
                .instanceProvider(new ModelElementTypeBuilder.ModelTypeInstanceProvider<Priority>() {
                    @Override
                    public Priority newInstance(ModelTypeInstanceContext instanceContext) {
                        return new PriorityImpl(instanceContext);
                    }
                });
        orderAttribute = typeBuilder.integerAttribute("order").namespace(CAMUNDA_NS).build();
        typeBuilder.build();
    }

    @Override
    public Integer getOrder() {
        return orderAttribute.getValue(this);
    }

    @Override
    public void setOrder(Integer order) {
        orderAttribute.setValue(this, order);
    }
}
